package com.shfl.servermanage.logic.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageInfo;
import com.shfl.servermanage.logic.bean.SysServer;
import com.shfl.servermanage.logic.bean.SysServerBt;
import com.shfl.servermanage.logic.bean.SysServerShell;
import com.shfl.servermanage.logic.constants.CommonConstants;
import com.shfl.servermanage.logic.mapper.SysServerMapper;
import com.shfl.servermanage.logic.model.request.AddServerRequest;
import com.shfl.servermanage.logic.model.request.RemoveByIdRequest;
import com.shfl.servermanage.logic.model.response.SysServerPageResponse;
import com.shfl.servermanage.logic.service.SysServerBtService;
import com.shfl.servermanage.logic.service.SysServerService;
import com.shfl.servermanage.logic.service.SysServerShellService;
import com.shfl.servermanage.pagehelper.PageHelperUtils;
import com.shfl.servermanage.pagehelper.Pageable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SysServerServiceImpl implements SysServerService {

    @Autowired
    private SysServerMapper sysServerMapper;

    @Autowired
    private SysServerBtService sysServerBtService;

    @Autowired
    private SysServerShellService sysServerShellService;

    @Override
    public PageInfo<SysServerPageResponse> findPage(Pageable pageable, Map<String, Object> objectMap) {
        PageHelperUtils.startPageWithCount(pageable);
        return new PageInfo<>(sysServerMapper.findPage(objectMap));
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddServerRequest addServerRequest) {
        SysServer sysServer = new SysServer();
        sysServer.setIpAddress(addServerRequest.getIpAddress());
        Date startTime = DateUtil.parse(addServerRequest.getStartTimeValue(), DatePattern.NORM_DATETIME_PATTERN);
        sysServer.setStartTime(startTime);
        Date endTime = DateUtil.parse(addServerRequest.getEndTimeValue(), DatePattern.NORM_DATETIME_PATTERN);
        sysServer.setEndTime(endTime);
        sysServerMapper.insertSelective(sysServer);

        SysServerBt sysServerBt = new SysServerBt();
        sysServerBt.setServiceId(sysServer.getId());
        sysServerBt.setRootDirectory(CommonConstants.BT_ROOT_DIRECTORY);
        sysServerBtService.insertSelective(sysServerBt);

        SysServerShell sysServerShell = new SysServerShell();
        sysServerShell.setServiceId(sysServer.getId());
        sysServerShell.setShellIp(addServerRequest.getIpAddress());
        sysServerShell.setShellName(addServerRequest.getIpAddress());
        sysServerShellService.insertSelective(sysServerShell);
    }

    @Override
    public void remove(RemoveByIdRequest removeByIdRequest) {
        sysServerMapper.logicalDeleteByPrimaryKey(removeByIdRequest.getId());
    }

    @Override
    public List<SysServerPageResponse> findList(Map<String, Object> objectMap) {
        return sysServerMapper.findList(objectMap);
    }
}
